package boids.predators;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import boids.preys.Prey;

import util.Vector;


public class TestPredator implements Predator
{
	private Vector location, velocity;
	
	private static double SIZE = 1;
	private static double PERCEPTION_RADIUS = 50;
	private static double SEPARATION_RADIUS = 5;
	private static double MAX_SPEED = 2;
	private static double MAX_FORCE = 1;
	private static double MIN_FORCE = MAX_FORCE * 0.25;
	
	public TestPredator(Vector someLocation)
	{
		location = someLocation.clone();
		velocity = new Vector();
	}
	
	public void update(ArrayList<Prey> prey)
	{
		velocity = new Vector();
		Vector acceleration = preyCenter(prey);
		velocity.add(acceleration);
		velocity.limit(MAX_SPEED);
		location.add(velocity);
	}
	
	public void manualUpdate(Vector direction)
	{
		direction.normalize();
		direction.multiply(MAX_FORCE);
		
		velocity.add(direction);
		velocity.limit(MAX_SPEED);
		
		location.add(velocity);
	}
	
	private Vector preyCenter(ArrayList<Prey> prey)
	{
		Vector preyMean = new Vector();
		
		for (Prey p : prey)
		{
			preyMean.add(p.getLocation());
		}
		
		preyMean.divide(prey.size());
		
		Vector preyMeanForce = Vector.substract(preyMean, location);
		preyMeanForce.limit(MAX_FORCE);
		
		return preyMeanForce;
	}
	
	public void setLocation(Vector someLocation)
	{
		location = someLocation.clone();
	}
	
	public Vector getLocation()
	{
		return location.clone();
	}
	
	public Vector getVelocity()
	{
		return velocity.clone();
	}
	
	public boolean isAlarmed()
	{
		return false;
	}
	
	public void draw(Graphics2D g2)
	{
		Rectangle2D.Double body = new Rectangle2D.Double(location.getX() - SIZE/2,
								location.getY() - SIZE/2,
								SIZE,
								SIZE);

		g2.setColor(Color.RED);
		g2.fill(body);
		//g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.draw(body);
	}
}
